package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.service.MarketGoodsService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.core.io.InputStreamResource
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.math.BigDecimal

@RestController
@RequestMapping("markets/{marketId}/goods")
class MarketsGoodsController(
    private val marketGoodsService: MarketGoodsService
) {

    data class QueryMarketGoodsRequest(
        val name: String?,
        val barcode: String?,
        val categoryIds: List<Int> = emptyList(),
        val brandIds: List<Int> = emptyList(),
        val nameOrBarcode: String?,
        val factoryIds: List<Int> = emptyList(),
    )

    @GetMapping
    fun queryMarketsGoods(@PathVariable marketId: Int,request: QueryMarketGoodsRequest, pageable: Pageable):Page<QueryMarketsGoodsResponse> {
        return marketGoodsService.queryMarketsGoods(marketId,request,pageable)
    }

    data class QueryMarketsGoodsResponse(
        val amountInMarket:Int,
        val amount:Int,
        val id:Int,
        val name:String,
        val price:BigDecimal,
        val barcode:String,
    )

    @GetMapping("brands")
    fun getAllMarketGoodsBrands(@PathVariable marketId: Int): List<IntIdAndName> {
        return marketGoodsService.getAllMarketGoodsBrands(marketId)
    }

    @GetMapping("categories")
    fun getAllMarketGoodsCategories(@PathVariable marketId: Int): List<IntIdAndName> {
        return marketGoodsService.getAllMarketGoodsCategories(marketId)
    }

    @GetMapping("factories")
    fun getAllMarketGoodsFactories(@PathVariable marketId: Int): List<IntIdAndName> {
        return marketGoodsService.getAllMarketGoodsFactories(marketId)
    }

    @GetMapping("excel")
    fun exportMarketsGoodsToExcel(@PathVariable marketId: Int,request: QueryMarketGoodsRequest):InputStreamResource{
        return marketGoodsService.exportMarketsGoodsToExcel(marketId,request)
    }


}